package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.DishFlavor;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
@Transactional
public class DishServiceImpl implements DishService {
    //菜品信息表
    @Autowired
    private DishMapper dishMapper;
    //菜品分类表
    @Autowired
    private CategoryMapper categoryMapper;
    //菜品口味信息新
    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    //查询所有
    @Override
    public Page<Dish> findByPage(String name, Integer pageNum, Integer pageSize) {
        //1.从菜品表中根据name分页查询
        //分页对象 泛型就是要分页的表对应的实体类
        Page<Dish> page = new Page<>(pageNum, pageSize);

        //封装条件构造器 准备进行条件查询
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<Dish>();
        //根据搜索框传入的name进行模糊查询  此处要加入if判断 name是否为空
        wrapper.like(StringUtils.isNotEmpty(name), Dish::getName, name);
        //执行分页查询
        page = dishMapper.selectPage(page, wrapper);
        System.out.println(page);

        //2.遍历菜品列表 使用分页查询到的数据都保存在Records集合中
        List<Dish> dishList = page.getRecords();
        //使用for循环遍历之前先进行非空判断 防止空指针 遍历到的dish就是一条菜品信息
        if (CollectionUtil.isNotEmpty(dishList)) {
            for (Dish dish : dishList) {
                //3.得到每个菜品的category_id 从分类表中根据分类id查询分类信息 主键查询
                Category category = categoryMapper.selectById(dish.getCategoryId());
                //将这个菜品对应的分类名称赋值到dish对象中 返回给前端
                dish.setCategoryName(category.getName());
                //4.得到每个菜品id,从口味表中根据菜品id查询口味列表 使用条件查询
                LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<DishFlavor>();
                wrapper1.eq(DishFlavor::getDishId, dish.getId());
                List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(wrapper1);
                dish.setFlavors(dishFlavors);
            }
        }
        return page;
    }

    //新增
    @Override
    public void save(Dish dish) {
        //1.获取菜品的基本信息 保存到dish表 获取到生成后的id
        dishMapper.insert(dish);//mybatisplus自动返回主键
        log.info("保存后的菜品主键是:{}", dish.getId());
        //2.获取菜品口味列表 进行遍历 将遍历的结果赋值到dishflavor对象中
        List<DishFlavor> flavorList = dish.getFlavors();

        //循环遍历dish对象中的口味信息集合 封装到dishFlavor对象中 保存到数据库
        if (CollectionUtil.isNotEmpty(flavorList)) {
            for (DishFlavor dishFlavor : flavorList) {
                //设置口味表的dish_id是菜品表的id
                dishFlavor.setDishId(dish.getId());
                //口味表的name和value集合中都有,加上dish_id就全了
                //遍历出来一个元素直接使用insert保存到数据库
                dishFlavorMapper.insert(dishFlavor);

            }
        }

    }

    @Override
    public Dish findById(Long id) {
        //1.根据id,从菜品表查询菜品基本信息
        Dish dish = dishMapper.selectById(id);
        //2.查询分类表信息
        Long categoryId = dish.getCategoryId();
        Category category = categoryMapper.selectById(categoryId);//根据dish表的id找到对应的category表信息
        //将菜品的分类名封装到dish对象中
        dish.setCategoryName(category.getName());
        //3.查询口味表信息
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<DishFlavor>();
        wrapper.eq(DishFlavor::getDishId, dish.getId());//根据dish表的DishId找到对应的dishF_lavor表信息
        List<DishFlavor> dishFlavorList = dishFlavorMapper.selectList(wrapper);
        dish.setFlavors(dishFlavorList);//将查找到的dishF_lavor表信息封装到dish的口味属性中
        return dish;

    }

    @Override
    public void update(Dish dish) {
        //1.修改菜品表信息
        dishMapper.updateById(dish);
        //2.删除口味表中当前菜品的相关信息
        //delete from dish_flavor where dish_id = #{}
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId, dish.getId());
        dishFlavorMapper.delete(wrapper);

        //3.将修改后的口味信息保存到口味表中
        List<DishFlavor> flavorlist = dish.getFlavors();
        if (CollectionUtil.isNotEmpty(flavorlist)) {
            for (DishFlavor dishFlavor : flavorlist) {
                //设置菜品的id
                dishFlavor.setDishId(dish.getId());
                dishFlavorMapper.insert(dishFlavor);
            }
        }
    }

    @Override
    public void updateStatus(Integer status, Long[] ids) {
        //修改菜品状态
        for (Long id : ids) {
            Dish dish = new Dish();
            dish.setId(id);
            dish.setStatus(status);
            //根据主键修改状态
            dishMapper.updateById(dish);
        }
        /*if (status == 0) {
            //遍历数组,得到每一个参数id,根据id修改商品状态
            for (Long id : ids) {
                //设置更新后的表格数据
                Dish dish = new Dish();
                dish.setStatus(0);
                //设置更新条件
                LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(Dish::getId, id);
                //执行更新
                dishMapper.update(dish, wrapper);
            }
        }
        if (status == 1) {
            //遍历数组,得到每一个参数id,根据id修改商品状态
            for (Long id : ids) {
                //设置更新后的表格数据
                Dish dish = new Dish();
                dish.setStatus(1);
                //设置更新条件
                LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(Dish::getId, id);
                //执行更新
                dishMapper.update(dish, wrapper);
            }
        }*/
    }

    @Override
    public ResultInfo deleteById(Long[] ids) {
        //对要删除的id进行判断,如果有一个是在售状态就不允许删除
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        //构造条件
        wrapper.in(Dish::getId, ids).eq(Dish::getStatus, 1);
        //查询符合条件的个数
        Integer count = dishMapper.selectCount(wrapper);
        if (count > 0) {
            return ResultInfo.error("当前菜品正在销售中,无法删除!");
        }
        for (Long id : ids) {
            //删除口味表信息
            LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(DishFlavor::getDishId, id);
            dishFlavorMapper.delete(wrapper1);
            //删除菜品表信息
            dishMapper.deleteById(id);

        }
        return ResultInfo.success(null);
   /*     String s = "";
        //遍历数组 根据id查询菜品的状态
        for (Long id : ids) {
            LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Dish::getId, id);
            Dish dish = dishMapper.selectOne(wrapper);
            s += dish.getStatus();
            //如果要删除的菜品中有启售状态的 不进行删除
            if (s.contains("1")) {
                return ResultInfo.error("当前菜品正在销售中,无法删除!");
            }
        }
        //进行到这里说明这些菜品都是停售状态 同时删除菜品表和口味表
        for (Long id : ids) {
            //删除口味表中的信息
            LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(DishFlavor::getDishId, id);
            dishFlavorMapper.delete(wrapper);
            //删除菜品表中的信息
            dishMapper.deleteById(id);
        }


        return ResultInfo.success(null);*/
    }

    @Override
    public List<Dish> findBycategoryId(Long categoryId, String name) {
        //查询菜品基本信息
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dish::getCategoryId, categoryId)
                .eq(Dish::getStatus, 1);
        List<Dish> dishList = dishMapper.selectList(wrapper);
        //查询菜品口味信息
        for (Dish dish : dishList) {
            //遍历菜品集合 每个dish就是一个菜品
            LambdaQueryWrapper<DishFlavor> wrapper1 = new LambdaQueryWrapper<>();
            //根据菜品的id查询口味表的信息,将返回的口味信息再封装到dish对象中
            wrapper1.eq(DishFlavor::getDishId, dish.getId());
            dish.setFlavors(dishFlavorMapper.selectList(wrapper1));
        }
        //如果前端传递了菜品名称 按菜品名称模糊查询返回结果
        if (StringUtils.isNotEmpty(name)) {
            LambdaQueryWrapper<Dish> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.like(StringUtils.isNotEmpty(name), Dish::getName, name);
            return dishMapper.selectList(wrapper1);
        }
        return dishList;


    }

  /*  @Override
    public List<Dish> findByName(String name) {


    }*/
}
